package com.dengqi.mymodule.activity.chenj;

import android.os.Bundle;
import android.view.Gravity;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.ShapeBadgeItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;
import com.dengqi.mymodule.tools.ActivityUtils;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


public class NavActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener{


    BottomNavigationBar bottomNavigationBar;
    private Fragment currentFragment;
    private Fragment homeFragment;
    private Fragment typeFragment;
    private Fragment findFragment;
    private Fragment mineFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chenj_nav);

        bottomNavigationBar = findViewById(R.id.bottom_navigation_bar);

        //为item添加角标
        TextBadgeItem mTextBadgeItem = new TextBadgeItem()
                .setBorderWidth(4)
                .setBackgroundColorResource(R.color.colorAccent)
                .setAnimationDuration(200)
                .setText("3")
                .setHideOnSelect(false);
        //通过hide/show显示隐藏
//        mTextBadgeItem.hide();
//        mTextBadgeItem.show();

        //为item添加角标
        ShapeBadgeItem mShapeBadgeItem = new ShapeBadgeItem()
                .setShapeColorResource(R.color.colorPrimary)
                .setGravity(Gravity.TOP | Gravity.END)
                .setHideOnSelect(false);

        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED); //模式跟背景的设置都要在添加tab前面，不然不会有效果。
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.setActiveColor(R.color.red);//选中颜色 图标和文字
        bottomNavigationBar.setInActiveColor(R.color.black);//默认未选择颜色
        bottomNavigationBar.setBarBackgroundColor(R.color.white);

        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_home, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.ic_type, "类型"))
                .addItem(new BottomNavigationItem(R.drawable.ic_find, "发现").setBadgeItem(mTextBadgeItem))
                .addItem(new BottomNavigationItem(R.drawable.ic_mine, "我的").setBadgeItem(mShapeBadgeItem))
                .setFirstSelectedPosition(0)
                .initialise(); //所有的设置需在调用该方法前完成
        bottomNavigationBar.setTabSelectedListener(this);

        //fragment
        switchFragment(0);

    }


    @Override
    public void onTabSelected(int position) {
        switchFragment(position);
    }

    @Override
    public void onTabUnselected(int position) {
    }

    @Override
    public void onTabReselected(int position) {
    }

    private void switchFragment(int position) {
        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = NavFragment.newInstance("首页");
                }
                switchFragment(homeFragment);
                break;
            case 1:
                if (typeFragment == null) {
                    typeFragment = NavFragment.newInstance("类型");
                }
                switchFragment(typeFragment);

                break;
            case 2:
                if (findFragment == null) {
                    findFragment = NavFragment.newInstance("发现");
                }
                switchFragment(findFragment);
                break;
            case 3:
                if (mineFragment == null) {
                    mineFragment = NavFragment.newInstance("我的");
                }
                switchFragment(mineFragment);
                break;
        }
    }

    private void switchFragment(Fragment targetFragment) {
        if (currentFragment == targetFragment) {
            return;
        }
        ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), targetFragment, R.id.framelayout);
        currentFragment = targetFragment;
    }
}
